package com.comum.javahomework420242025.repository;

import com.comum.javahomework420242025.pojo.Contract;
import com.comum.javahomework420242025.pojo.Quote;
import org.apache.ibatis.annotations.Select;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface ContractRepository extends JpaRepository<Contract,Long> {
    @Query("select c from contract c where " +
            "c.quote.chance.user.area.department=:department and (" +
            "cast(c.id as string) like %:str% or c.name like %:str% or " +
            "c.quote.chance.clientCompany.clientcompanyname like %:str% or " +
            "c.signedtime like %:str% or c.deliverytime like %:str% or " +
            "cast(c.quote.allprices as string) like %:str% or c.comment like %:str%)")
    Page<Contract> getContracts(String str, String department, Pageable pageable);

    @Query("select c.quote.chance.clientCompany.id from contract c " +
            "where c.id=:contractid")
    Long getClientId(Long contractid);
    @Query("select c.quoteid from contract c where " +
            "c.id=:valueOf")
    Long findQuoteId(Long valueOf);
    @Query("select c from contract c where " +
            "c.quote.chance.clientCompany.id=:clientcompanyid and (" +
            "c.name like %:str% or cast(c.id as string) like %:str% or " +
            "c.quote.chance.clientCompany.areaname like %:str% or " +
            "c.signedtime like %:str% or c.comment like %:str%)")
    List<Contract> getClientsComesByClientCompanyid(String str, String clientcompanyid);
    @Query("select c from contract c where " +
            "c.quoteid=:id")
    List<Contract> findByQuoteId(String id);
}
